package com.xiaocg.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class JdkProxy {
    public static void main(String[] args) {
        Class[] classes = {UserDao.class};
        UserDao userDao = new UserDaoImpl();
        Object o = Proxy.newProxyInstance(JdkProxy.class.getClassLoader(), classes, new UserDaoProxy(userDao));

        UserDao userDaoProxy = (UserDao) o;
        int result = userDaoProxy.add(1,2);

        System.out.println("result:"+result);

    }
}

class UserDaoProxy implements InvocationHandler{

    private Object object;

    public UserDaoProxy(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("方法之前"+ Arrays.toString(args));
        Object res = method.invoke(object, args);
        System.out.println("方法之后");
        return res;
    }
}
